package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.impl.TeacherServiceImpl;
import com.itheima.edu.info.manager.utils.AgeUtils;
import com.itheima.edu.info.manager.utils.InputUtils;

import java.util.Scanner;

public abstract class BaseTeacherController {
    Scanner sc = new Scanner(System.in);
    TeacherServiceImpl teacherServiceImpl = new TeacherServiceImpl();
    //开启学生管理系统,并展示菜单
    public void start() {
        teacherLoop:while (true){
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    System.out.println("添加");
                    addTeacher();   //add
                    break;
                case "2":
                    System.out.println("删除");
                    delectTeacher();    //delect
                    break;
                case "3":
                    System.out.println("修改");
                    updataTeacher();    //updata
                    break;
                case "4":
                    System.out.println("查询");
                    selectTeacher();   //select
                    break;
                case "5":
                    System.out.println("感谢您使用学生管理系统, 再见!");
                    break teacherLoop;
                default:
                    System.out.println("您的输入有误, 请重新输入");
                    break;
            }
        }
    }

    private void selectTeacher() {
        System.out.println("请输入要查找的id:(输入\\all指令 查询全部)");
        String id = sc.next();
        if ("\\all".equals(id)){
            Teacher[] allTeacher = teacherServiceImpl.findAllTeacher();
            if (allTeacher != null){
                System.out.println("工号\t\t姓名\t年龄\t\t生日");
                for (int i = 0; i < allTeacher.length; i++) {
                    if (allTeacher[i] != null){
                        System.out.println(allTeacher[i].getId() + "\t" + allTeacher[i].getName() + "\t" + allTeacher[i].getAge() + "\t\t" + allTeacher[i].getBirthday());
                    }
                }
                return;
            }else {
                System.out.println("当前系统无数据");
                return;
            }
        }
        while (true){
            /*
             *  去业务层查找学号是否存在
             *   存在返回true 不存在返回false
             * */
            boolean flag = teacherServiceImpl.isExists(id);
            if (flag){
                selectTeacher(id);
                break;
            }else {
                System.out.println("该教师信息不存在,请重新输入:");
                id =  sc.next();
            }
        }
    }

    private void selectTeacher(String id) {
        Teacher teacher = teacherServiceImpl.selectTeacher(id);
        if (teacher == null){
            System.out.println("查找失败");
        }else {
            System.out.println("工号\t\t姓名\t年龄\t\t生日");
            System.out.println(teacher.getId() + "\t" + teacher.getName() + "\t" + teacher.getAge() + "\t\t" + teacher.getBirthday());
        }


    }

    private void updataTeacher() {
        System.out.println("请输入id:");
        String id = sc.next();
        while (true){
            /*
             *  去业务层查找学号是否存在
             *   存在返回true 不存在返回false
             * */
            boolean flag = teacherServiceImpl.isExists(id);
            if (flag){
                break;
            }else {
                System.out.println("该工号不存在,请重新录入(放弃录入请输入quit):");
                id =  sc.next();
                if (id.equals("quit"))
                    break;
            }
        }

        Teacher teacher = inputTeacherInfo(id);

        Teacher teacher1 = teacherServiceImpl.updataTeacher(teacher);
        System.out.println("工号\t\t姓名\t年龄\t\t生日");
        System.out.println(teacher1.getId() + "\t" + teacher1.getName() + "\t" + teacher1.getAge() + "\t\t" + teacher1.getBirthday());

    }

    private void delectTeacher() {
        System.out.println("请输入id:");
        String id = sc.next();
        lo:while (true){
            boolean flag = teacherServiceImpl.isExists(id);
            if (flag){
                boolean soult = teacherServiceImpl.delectTeacherById(id);
                if (soult) {
                    System.out.println("删除成功");
                   }else
                    System.out.println("删除失败");
                break;
            }else {
                Teacher[] teachers = teacherServiceImpl.findAllTeacher();
                for (int i = 0; i < teachers.length; i++) {
                    if (teachers[i] != null){
                        System.out.println("该工号不存在,请重新输入");
                        id =  sc.next();
                        break lo;
                    }else {
                        System.out.println("系统内无数据");
                    }
                }
            }
        }

    }

    private void addTeacher() {
        System.out.println("请输入id:");
        String id = sc.next();

        while (true){
            /*
             *  去业务层查找工号是否存在
             *   存在返回true 不存在返回false
             * */
            boolean flag = teacherServiceImpl.isExists(id);
            if (!flag){
                break;
            }else {
                System.out.println("工号已被占用,请重写输入");
                id =  sc.next();
            }
        }

        Teacher teacher = inputTeacherInfo(id);

        boolean result = teacherServiceImpl.addTeacher(teacher);
        if (result == true){
            System.out.println("添加成功");
        }else
            System.out.println("添加失败");
    }

    private Teacher inputTeacherInfo(String id){

        System.out.println("请输入姓名:");
        String name = sc.next();
        //System.out.println("请输入年龄(x岁):");
        //String age = sc.next();
        //String age = InputUtils.inputRule("\\d+岁");
        System.out.println("请输入出生日期(xxxx-xx-xx):");
        //String birthday = sc.next();
        String birthday = InputUtils.inputRule("\\d{4}-\\d{2}-\\d{2}");
        String age = AgeUtils.getAge(birthday);
        Teacher teacher = new Teacher(id,name,age,birthday);
        return teacher;
    }



}
